﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DALayer
{
    class DBHelper
    {

       // private const string strCnn = "Data Source=.;Initial Catalog=TestDB;Integrated Security=True;";

        //请去掉注释，无需更改

        private static string connStr = "server =.; database = CarShare; Integrated Security = true ";
        //private static string connStr = "server =LAPTOP-FMUCDJLD\\SQLEXPRESS; database = CarShare; Integrated Security = true ";

        //创建数据库连接对象
        public static SqlConnection conn = new SqlConnection(connStr);

        public static DataTable SelectByAdapter(string sql)
        {
            try
            {
                //创建数据适配器对象
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                //创建数据集对象
                DataTable dt = new DataTable();
                //结果注入数据集
                da.Fill(dt);
                //返回数据集
                return dt;
            }
            catch (Exception ex)
            {

                return null;//返回空
            }
            finally
            {
                //关闭连接
                conn.Close();
            }
           
        }

        public static int UTNonQuery(string sql)
        {
            try
            {
                //打开连接
                conn.Open();
                //创建命令对象
                SqlCommand cmd = new SqlCommand(sql, conn);
                //执行ExecuteNonQuery()
                int number = cmd.ExecuteNonQuery();
                return number;
            }
            catch (Exception)
            {
                return -1;//执行失败
            }
            finally
            {
                //关闭连接
                conn.Close();
            }
            
        }


    }
}
